<?php

namespace plugin\csauto\app\controller;

use plugin\csauto\app\model\NcServerModel;
use support\Request;
use support\Response;
use plugin\admin\app\controller\Crud;

/**
 * NC服务器管理
 */
class NcServersController extends Crud
{
    /**
     * @var NcServerModel
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new NcServerModel;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        // 检查是否有查询参数
        $type = request()->get('type');
        if ($type === 'select') {
            return $this->select(request());
        }

        return view('ncservers/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     */
    public function insert(Request $request): Response
    {
        if ($request->isPost()) {
            $data = $request->post();
            // 处理密码字段
            if (isset($data['scp_password']) && $data['scp_password'] === '••••••••') {
                unset($data['scp_password']);
            }
            return parent::insert($request);
        }
        return view('ncservers/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     */
    public function update(Request $request): Response
    {
        if ($request->isPost()) {
            $data = $request->post();
            // 处理密码字段
            if (isset($data['scp_password']) && $data['scp_password'] === '••••••••') {
                unset($data['scp_password']);
            }
            return parent::update($request);
        }
        return view('ncservers/update');
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        return parent::delete($request);
    }
}
